package com.danieltoms.gcSync.screens;

import java.io.IOException;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.RichTextField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.IntHashtable;

import com.danieltoms.gcSync.GcSyncConstants;
import com.danieltoms.gcSync.GcSyncStore;

public class OptionsScreen extends MainScreen
{
	private ObjectChoiceField _whenSync = null;
	private ObjectChoiceField _onConflicts = null;

	private MenuItem m_helpItem = new MenuItem("Help", 10, 10)
	{
		public void run()
		{
			// push help
		}
	};

	private MenuItem m_saveItem = new MenuItem("Save", 20, 20)
	{
		public void run()
		{

			try
			{
				save();
			}
			catch (IOException io)
			{
				throw new RuntimeException(io.getMessage());
			}
			close();
		}
	};

	Object[] _syncChoices = { "Automatic", "Manual" };	
	Object[] _conflictChoices = { "Google Wins", "BlackBerry Wins" };
	
	// invoked automagically by default close behaviour
	public void save() throws IOException
	{
		int syncOption=0;
		int conflictOption=0;	

		if(_whenSync.getSelectedIndex()==0)
			syncOption=GcSyncConstants.OPTION_SYNC_AUTOMATIC;
		else if(_whenSync.getSelectedIndex()==1)
			syncOption=GcSyncConstants.OPTION_SYNC_MANUAL;		
		
		if(_onConflicts.getSelectedIndex()==0)
			conflictOption=GcSyncConstants.OPTION_CONFLICT_GOOGLE;
		else if(_onConflicts.getSelectedIndex()==1)
			conflictOption=GcSyncConstants.OPTION_CONFLICT_BLACKBERRY;		
		
		GcSyncStore.instance().storeConflictOption(conflictOption);
		GcSyncStore.instance().storeSyncOption(syncOption);
		GcSyncStore.instance().commit();
	}

	private MenuItem m_resetSyncItem = new MenuItem("Reset Sync", 30, 30)
	{
		public void run()
		{

		}
	};

	protected void makeMenu(Menu menu, int instance)
	{
		super.makeMenu(menu, instance);
		// todo: if logged in show log out button, if logged out, show log in.
		menu.add(m_helpItem);
		menu.add(m_saveItem);
		menu.add(m_resetSyncItem);
		// menu.addSeparator();
		// menu.add(m_closeItem);

	}

	/**
	 * Add the title and a simple richtext field
	 */
	public OptionsScreen()
	{
		super(DEFAULT_CLOSE);
		drawScreen();
		System.out.println("create options screen");
	}

	private void log(String string)
    {
	    System.out.println(string);
	    
    }

	private void drawScreen()
	{
		// todo remove this and just keep the handle to the items and update
		// them as appropriate
		this.deleteAll();
		// add title
		setTitle(new LabelField("Options", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));

		// add sign in message
		this.add(new RichTextField("Sync", Field.NON_FOCUSABLE));
		this.add(new SeparatorField());
		this.add(new RichTextField("", Field.NON_FOCUSABLE));

		int syncOption=GcSyncStore.instance().getSyncOption();
		int conflictOption=GcSyncStore.instance().getConflictOption();
		int syncSelection=0;
		int conflictSelection=0;
		
		if(syncOption==GcSyncConstants.OPTION_SYNC_AUTOMATIC)
		{
			syncSelection=0;
		}
		else if(syncOption==GcSyncConstants.OPTION_SYNC_MANUAL)
		{
			syncSelection=1;	
		}
		
		
		if(conflictOption==GcSyncConstants.OPTION_CONFLICT_GOOGLE)
			conflictSelection=0;
		else if(conflictOption==GcSyncConstants.OPTION_CONFLICT_BLACKBERRY)
			conflictSelection=1;
			
		_whenSync = new ObjectChoiceField("When to Sync", _syncChoices, syncSelection);
		this.add(_whenSync);

		_onConflicts = new ObjectChoiceField("On Conflicts", _conflictChoices, conflictSelection);
		this.add(_onConflicts);

	}

}
